'use strict';

/**
 * @ngdoc function
 * @name jgappApp.controller:FinanceFYhbhCtrl
 * @description
 * # FinanceFYhbhCtrl
 * Controller of the jgappApp
 */
angular.module('jgappApp')
    .controller('FinanceFYhbhCtrl', function($scope, openApi, $rootScope, Util, $location, $routeParams, FormSubmit) {
        // 底部关联模块类型选框添加
        $scope.sheetObj = {};
        var accountTypeList = [{
            key: "投标保函",
            value: "bid_apply"
        }, {
            key: "履约保函",
            value: "select_contract_by_bank"
        }, {
            key: "信贷证明",
            value: "bid_apply"
        }, {
            key: "预付款保函",
            value: "select_contract_by_bank"
        }, {
            key: "其他",
            value: "other"
        }];

        var AccountTypeObj = {};
        AccountTypeObj.sheettitle = "账户类型";
        AccountTypeObj.tabList = accountTypeList
        AccountTypeObj.callback = function(item) {
            $scope.param.guaranteeType = item.selectTabContent
            $scope.param.serachtext = item.selectTab
                // 选择后清空工程名称值
            $scope.param.projectName = "";
            switch (item.selectTab) {
                case "bid_apply":
                    $scope.param.holder = "请选择投标申请单";
                    break;
                case "select_contract_by_bank":
                    $scope.param.holder = "请选择工程合同";
                    break;
                default:
                    $scope.param.holder = "请输入工程名称"
                    break;
            }
        }
        $scope.sheetObj.AccountTypeObj = AccountTypeObj;
        $scope.showSheet = function(type) {
            var sheetObj = $scope.sheetObj[type]
            $scope.sheetObj.type = type
            $scope.sheetObj.tabList = sheetObj.tabList
            $scope.sheetObj.sheettitle = sheetObj.sheettitle
            $scope.sheetObj.ifshowSheet = true
            $rootScope.showMasker = true
        };
        $scope.chooseinfo = function() {
            var guaranteeType = $scope.param.guaranteeType
            if (guaranteeType == "其他" || !guaranteeType) {
                return
            }
            var searchtext;
            accountTypeList.forEach(function(item, index) {
                if (item.key == guaranteeType) {
                    searchtext = item.value
                    return
                }
            })
            var relModuleObj = {
                name: "FinanceFZjfyCtrl",
                selectTab: searchtext,
                selectTabContent: $scope.param.holder
            };
            // 清掉关联的编号
            var params = $scope.param;
            Util.savaParamForReview(params, $routeParams.id);
            Util.savaDataforsearch(relModuleObj);
            $location.path("finance/choosepeople")
        };

        $scope.submit = function() {
            if (!$scope.param.guaranteeType) {
                Util.Toast("请选择保函类型");
                return;
            }
            if (!$scope.param.amount) {
                if ($scope.param.amount !== 0) {
                    Util.Toast("请填写保函开具金额");
                    return;
                }
            }
            if (!$scope.param.projectName) {
                Util.Toast("请选择工程名称");
                return;
            }
            // 提交
            var urlParam = "?operationId=-2" + "&&processId=" + $routeParams.id + "&&url=" + openApi.bankGuarantee
            var options = {};
            options.urlParam = urlParam;
            var myDest = {};
            options.param = angular.copy($scope.param, myDest);
            var TVtype;
            TVtype = Util.getTVType(); //根据页面的来源来判断是发起的流程还是修改的流程，如果是发起的则不管，如果是修改的则获取数据用来回显
            // 如果是修改页面的话则提交的地址不一样，参数一样
            if (TVtype == Util.UPDATE) {
                options.url = openApi.ip + openApi.bankGuarantee
            }
            new FormSubmit(options);
        };

        var init = function() {
            // 获取回显的详细信息
            Util.getDetailData($routeParams.id, function(data) {
                $scope.param = data;
            });
            // 显示审批记录判断
            Util.showloglist($routeParams.id, function(data) {
                $scope.logData = data;
            });
        };
        // 初始化
        init();
    });
